// pages/submitorder/index.js
import ddt from '../../api/axios.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: ['一份', '二份', '三份', '四份', '五份'],
    pickershow: false,
    can: '必选',
    address: '',
    name: '',
    phone: '',
    orderid: '',
    userinfo: wx.getStorageSync('userinfo'),
    detail: [],
    order: '',
    remark: "",
    shopin: "",
    shopin1:'',
    userid:"",
  },
  shopinfo(id) {
    ddt('post', '/api/settlement/page', {
      orderid: id
    }).then(res => {
      this.setData({
        shopin1: res.data.data.shopinfo,
        shopin: res.data.data.orderinfo,
        detail: res.data.data.orderdetail
      })
    })
  },
  pay() {
    ddt('post', '/api/wxpay', {
      order_id: this.data.orderid, //订单id
      addressid:this.data.address.id,
      remark: this.data.remark //备注
    }).then(res => {
      if (res.data.code == 1) {
        let params = res.data.data
        // params.signType="MD5"
        console.log(params)
        wx.requestPayment(params).then(res => {
          console.log('支付',res)
          wx.showModal({
            title: '提示',
            content: '支付成功',
            confirmText: '查看订单',
            cancelText: '回到首页',
            success(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '../order/index',
                })
              } else if (res.cancel) {
                wx.switchTab({
                  url: '../index/index',
                })
              }
            },
          })
        }, err => {
          wx.showToast({
            title: '支付失败',
            icon: "error"
          })
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  // 支付
  getPayment(orderId) {
    console.log("用户信息：", wx.getStorageSync('userinfo'));
    let openid = wx.getStorageSync('userinfo').openid
    ddt('post', "/applogin/wxpay", {
      openid,
      totalfee: JSON.stringify(this.data.order.totalfee*1 + this.data.qisong*1) ,
      orderid: this.data.order.id
      // totalfee: 0.01
    }).then(res => {
      console.log(res)
      let params = res.data.data
      params.signType="MD5"
      wx.requestPayment(params).then(res => {
        wx.showModal({
          title: '提示',
          content: '支付成功',
          confirmText: '查看订单',
          cancelText: '回到首页',
          success(res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../order/index',
              })
            } else if (res.cancel) {
              wx.switchTab({
                url: '../index/index',
              })
            }
          },
        })
        ddt("post", "/applogin/afterpayorder", {
          orderid: orderId,
          recharge: 0, 
          onlinepay: JSON.stringify(this.data.order.totalfee)  
        }).then(res => {
          console.log("支付成功");
        })
      }, err => {
        wx.showToast({
          title: '支付失败',
          icon: "error"
        })
      })
    })
  },
  remarkinput(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  picker() {
    this.setData({
      pickershow: true
    })
  },
  onCancel() {
    this.setData({
      pickershow: false
    })
  },
  onConfirm(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
      can: event.detail.value,
      pickershow: false
    })
  },
  gopath1() {
    wx.navigateTo({
      url: '../address/index?id=' + 2
    })
  },
  gopath(e) {
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderid:options.orderid
    })
    this.shopinfo(options.orderid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
  },
  addressinfo() {
    ddt('get', `/api/user/addresslist`).then(res => {
      if (res.data.data.length == 0) {
        wx.showModal({
          title: '提示',
          content: '前往添加地址',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '../address/index?id=' + 2
              })
            }
          },
        })
      }
      this.setData({
        address: res.data.data[0]
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.infoparam == undefined) {
      this.addressinfo()
    } else {
      this.setData({
        address: this.data.infoparam
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})